package org.example.asm.easydatastructure;

import java.util.Scanner;
import java.util.TreeMap;

public class ManyListTest05 {
    private static TreeMap<Integer, Integer> hashMap = new TreeMap<>();
    private static int total =0; //注意必须用，用来计算数量，如果自己调用方法计算，则会超时

    public static void insertValue(int x) {
        // TODO: 实现插入逻辑
        if (hashMap.containsKey(x)) {
            hashMap.put(x, hashMap.get(x) + 1);
            total++;
        } else {
            hashMap.put(x, 1);
            total++;
        }

    }

    public static void eraseValue(int x) {
        // TODO: 实现删除逻辑
        if (hashMap.containsKey(x)) {
            if (hashMap.get(x) > 1) {
                hashMap.put(x, hashMap.get(x) - 1);
                total--;
            } else {
                hashMap.remove(x);
                total--;
            }

        }

    }

    public static int xCount(int x) {
        // TODO: 求x在集合中的个数
        return hashMap.getOrDefault(x, 0);
    }

    public static int sizeOfSet() {
        // TODO: 返回集合大小

        return total;
    }

    public static int getPre(int x) {
        // TODO: 实现找前驱
        Integer result = hashMap.lowerKey(x);
        return result == null ? -1 : result;
    }

    public static int getBack(int x) {
        // TODO: 实现找后继
        Integer result = hashMap.higherKey(x);
        return result == null ? -1 : result;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int q = scanner.nextInt();
        while (q-- > 0) {
            int op = scanner.nextInt();
            switch (op) {
                case 1:
                    insertValue(scanner.nextInt());
                    break;
                case 2:
                    eraseValue(scanner.nextInt());
                    break;
                case 3:
                    System.out.println(xCount(scanner.nextInt()));
                    break;
                case 4:
                    System.out.println(sizeOfSet());
                    break;
                case 5:
                    System.out.println(getPre(scanner.nextInt()));
                    break;
                case 6:
                    System.out.println(getBack(scanner.nextInt()));
                    break;
            }
        }
        scanner.close();
    }
}

